﻿$MET.Load.Install(function () {
    ChekPageRight(3001, 3); //验证页面访问权限

    //数采仪选择 表格
    $(function () {
        $('#DivDAU').datagrid({
            title: '数采仪',
            width: 1,
            height: 1,
            fit: true,
            fitColumns: true,
            singleSelect: true,
            columns: [[
					{ field: 'DAUNo', title: '仪器编号',  align: 'left' },
                    { field: 'DAUMN', title: 'MN编号', align: 'left' },
                    { field: 'IP', title: 'IP地址',  align: 'left' },
                    { field: 'LoginTime', title: '登录时间', align: 'left'
                        , formatter: function (value, rowData, rowIndex) {
                            if (value != null) {
                                return $MD.Date(value).toFormatString("yyyy/mm/dd hh:MM:ss");
                            }
                            else {
                                return value;
                            }
                        }
                    },
                    { field: '2', title: '更多',  align: 'center',
                        formatter: function (value, rowData, rowIndex) {
                            var MarkerName = rowData.MarkerName;
                            var PowerStatusName = rowData.PowerStatusName;
                            var ReserveStatusName = rowData.ReserveStatusName;
                            var IsOnLine = rowData.IsOnLine;

                            var Title = "仪器型号：" + MarkerName + "\n电源状态：" + PowerStatusName +
                                "\n备用电源状态：" + ReserveStatusName + "\n是否在线：" + IsOnLine;

                            return "<img alt='' title='" + Title + "' src='../../Image/Ico/More.png' style='width:25px;height:25px;'/></a>";
                        }
                    }
                ]],
            onClickRow: function (rowIndex, rowData) {
                //单击事件
                var DAUNo = rowData.DAUNo;
                var DAUMN = rowData.DAUMN;
                var DAUMarker = rowData.DAUMarker;
                var DAUPWD = rowData.PassWord;
                var DAUCN = rowData.CommandCN;
                //存入数值 供Hsitory框刷新及命令获取使用
                document.getElementById("DAUNo").value = DAUNo;
                document.getElementById("DAUMN").value = DAUMN;
                document.getElementById("DAUPWD").value = DAUPWD;

                $MCC.SetValue("SearchDAUNo", DAUNo);
                $MCC.SetValue("SearchDAUMN", DAUMN);
                $MCC.SetValue("SearchPWD", DAUPWD);
                //载入反控命令信息
                var RTUrl = "../../../Service/Expand/ReverseControl/ControlConsole/GetAllMarkerCommand.ashx";
                var RTData = "Marker=" + DAUMarker;
                $MRT.Ajax(false, RTUrl, RTData, function (xmlData, state, rowData) {
                    var Json = $MJO.GetObject(xmlData);
                    if (Json != null && Json.Successful == true) {
                        var Selt = $MEM("SeltCommand");
                        Selt.options.length = 0;
                        for (var I in Json.Data) {
                            var Item = Json.Data[I];
                            var Op = document.createElement("OPTION");

                            Op.value = Item.No;
                            Op.text = "[" + Item.CommandCN + "] " + Item.CommandName;
                            Selt.add(Op);
                        }
                        $MEM("BtnCommand").onclick();
                        //载入命令完成
                    }
                    else {
                        $.messager.alert("提示", "无任何控制命令信息.");
                    }
                }, rowData);
            }
        });
    });
    //实时数据 表格
    $(function () {
        $('#DivReal').datagrid({
            title: '实时数据',
            width: 1,
            height: 1,
            fit: true,
            fitColumns: true,
            singleSelect: true,
            columns: [[
					{ field: 'PollutantName', title: '污染物', width: 50, align: 'left' },
                    { field: 'Value', title: '实测', width: 35, align: 'left' },
                    { field: 'CountValue', title: '表显数', width: 50, align: 'left' },
                    { field: 'WorkStatusName', title: '状态', width: 30, align: 'center' },
                    { field: 'UpdataTime', title: '更新时间', width: 80, align: 'center'
                        , formatter: function (value, rowData, rowIndex) {
                            if (value != null) {
                                return $MD.Date(value).toFormatString("yyyy/mm/dd hh:MM:ss");
                            }
                            else {
                                return value;
                            }
                        }
                    }
                ]]
        });
    });
    //反控命令状态
    $(function () {
        $('#DivStatus').datagrid({
            title: '控制命令状态',
            width: 1,
            height: 1,
            fit: true,
            fitColumns: true,
            singleSelect: true,
            columns: [[
					{ field: 'CommandName', title: '命令类型',  align: 'center' },
                    { field: 'StatusName', title: '执行状态', align: 'center' },
                    { field: 'SenderUser', title: '发送者', align: 'center' },
                    {
                        field: 'SendTime', title: '发送时间', align: 'center'
                        , formatter: function (value, rowData, rowIndex) {
                            if (value != null) {
                                return $MD.Date(value).toFormatString("yyyy/mm/dd hh:MM:ss");
                            }
                            else {
                                return value;
                            }
                        }
                    }
                ]]
        });
    });


    //载入所有企业分组信息
    var RTUrl = "../../../Service/Basic/Search/GetAllPolluteSourceGroup.ashx";
    var RTData = "";
    $MRT.Ajax(true, RTUrl, RTData, function (xmlData, state) {
        var Json = $MJO.GetObject(xmlData);
        if (Json != null && Json.Successful == true) {
            var Selt = $MEM("SeltGroup");
            Selt.options.length = 0;
            for (var I in Json.Data) {
                var Op = document.createElement("OPTION");
                Op.value = Json.Data[I].No;
                Op.text = Json.Data[I].GroupName;
                Selt.add(Op);
            }

            if ($MCC.GetValue("BeforeGroupSelect") != null) {
                //载入历史查询数据
                $MEM("SeltGroup").value = $MCC.GetValue("BeforeGroupSelect");
            }
            else {
                $MCC.SetValue("BeforeGroupSelect", $MEM("SeltGroup").value);
            }
        }
        else {
            $.messager.alert("提示", "无任何分组信息.");
        }
    });

    //绑定事件
    $MEM("BtnSearch").onclick = function () {
        //获取数据
        var Group = $MEM("SeltGroup").value;
        var KeyName = $MEM("TxtKeyName").value;

        if (Group == "") {
            $.messager.alert("提示", "请选择企业分组.");
            return;
        }
        var RTUrl = "../../../Service/Basic/Search/SearchPolluteSourceDetail.ashx";
        var RTData = "Group=" + Group;
        RTData += "&KeyName=" + KeyName;
        $MRT.Ajax(true, RTUrl, RTData, function (xmlData) {
            var Json = $MJO.GetObject(xmlData);
            if (Json != null && Json.Successful != false) {

                var Selt = $MEM("SeltPolluteSource");
                Selt.options.length = 0;
                for (var I in Json.Data) {
                    var Op = document.createElement("OPTION");
                    Op.value = Json.Data[I].PolluteSourceNo;
                    Op.text = Json.Data[I].PolluteSourceName;
                    Selt.add(Op);
                }

                //记录并设置上次分类选择
                $MCC.SetValue("BeforeGroupSelect", Group);
                //触发污染源onchange事件
                $MEM("SeltPolluteSource").onchange();
            }
            else {
                $.messager.alert("提示", "无任何企业信息.");
            }
        });
    }

    $MEM("SeltPolluteSource").onchange = function () {
        var PolluteSourceNo = $MEM("SeltPolluteSource").value;
        if (PolluteSourceNo != null && PolluteSourceNo != -1) {
            var RTUrl = "../../../Service/Basic/Search/SearchDischargePort.ashx";
            var RTData = "PolluteSourceNo=" + PolluteSourceNo;
            $MRT.Ajax(true, RTUrl, RTData, function (xmlData) {
                var Json = $MJO.GetObject(xmlData);
                if (Json != null && Json.Successful != false) {

                    var Selt = $MEM("SeltDischargePort");
                    Selt.options.length = 0;
                    for (var I in Json.Data) {
                        var Op = document.createElement("OPTION");
                        Op.value = Json.Data[I].DischargePortNo;
                        Op.text = Json.Data[I].DischargePortName;
                        Selt.add(Op);
                    }
                    $MEM("SeltDischargePort").onchange();
                }
                else {
                    $.messager.alert("提示", "无任何排放口信息.");
                }
            });
        }
    }
    //选择按钮
    $MEM("SeltDischargePort").onchange = function () {
        var DischargePortNo = $MEM("SeltDischargePort").value;

        //查询相关数采仪信息
        var RTUrl = "../../../Service/Expand/ReverseControl/ControlConsole/GetAllDAUInformation.ashx";
        var RTData = "DischargePortNo=" + DischargePortNo;
        $MRT.Ajax(true, RTUrl, RTData, function (xmlData) {
            var Json = $MJO.GetObject(xmlData);
            if (Json != null && Json.Successful != false) {
                //填充数据
                var LoadData = $MJO.GetObject('{ "total": ' + Json.Data.length + ', "rows": [] }');
                LoadData.rows = Json.Data;
                //载入数据
                $('#DivDAU').datagrid('loadData', LoadData);
                if (Json.Data.length <= 0) {
                    $.messager.alert("提示", "无任何相关信息.");
                }
            }
        });
        //查询该通道口实时数据信息
        RTUrl = "../../../Service/Expand/ReverseControl/ControlConsole/GetAllRealInformation.ashx";
        RTData = "DischargePortNo=" + DischargePortNo;
        $MRT.Ajax(true, RTUrl, RTData, function (xmlData) {
            var Json = $MJO.GetObject(xmlData);
            if (Json != null && Json.Successful != false) {
                //填充数据
                var LoadData = $MJO.GetObject('{ "total": ' + Json.Data.length + ', "rows": [] }');
                LoadData.rows = Json.Data;
                //载入数据
                $('#DivReal').datagrid('loadData', LoadData);
            }
        });
    }

    $MEM("SeltCommand").onchange = function () {
        $MEM("BtnCommand").onclick();
    }
    $MEM("BtnCommand").onclick = function () {

        var CommandNo = $MEM("SeltCommand").value;
        if (CommandNo != null && CommandNo != -1) {
            $MEM("DivCommand").innerHTML = "载入中...";
            var RTUrl = "../../../Service/Expand/ReverseControl/ControlConsole/GetMarkerCommand.ashx";
            var RTData = "No=" + CommandNo;
            $MRT.Ajax(false, RTUrl, RTData, function (xmlData) {
                var Json = $MJO.GetObject(xmlData);
                if (Json != null && Json.Successful != false) {
                    var CMMD = $MD.String(Json.Data.CommandContent).Split("|")[1];

                    var HTML = $MD.String(Json.Data.CommandContent).Split("|")[0];

                    HTML = $M.Decode(HTML);



                    HTML = $MD.String(HTML).Replace("<#CommandCN>", Json.Data.CommandCN);
                    HTML = $MD.String(HTML).Replace("<#DAUMN>", document.getElementById("DAUMN").value);
                    HTML = $MD.String(HTML).Replace("<#DAUPWD>", document.getElementById("DAUPWD").value);
                    HTML = $MD.String(HTML).Replace("<#DAUNo>", document.getElementById("DAUNo").value);
                    HTML = $MD.String(HTML).Replace("<#CommandContent>", CMMD);

                    //复制文本

                    //$MEM("DivCommand").FillHTML(HTML);
                    $("#DivCommand").html(HTML);

                    //小时数据的控制
                    setInterval(function () {
                        InputObj = $(".spinner input")
                        $.each(InputObj, function (j, inputs) {
                            nowtime = $(inputs).val();
                            nowobject = nowtime.split(":");
                            newtime = "";
                            $.each(nowobject, function (i, item) {

                                if (i == 0) {
                                    newtime = item + ":00:00";
                                }

                            });

                            $(inputs).val(newtime);
                        })

                    }, 100)


                    setInterval(function () {
                        nowSdate = "";
                        nowEdate = "";
                        objCombo = $(".combo-value");
                        $.each(objCombo, function (i, item) {
                            if (i == 0) {
                                nowSdate = $(item).attr("value");
                            }
                            else {
                                nowEdate = $(item).attr("value");
                            }
                        });

                        nowSdate = Date.parse(nowSdate.replace(/-/g, "/"));
                        nowEdate = Date.parse(nowEdate.replace(/-/g, "/"));

                        Shour = new Date(nowSdate);
                        Ehour = new Date(nowEdate);
                        Bhour = parseInt((Ehour.getTime() - Shour.getTime()) / 1000 / 60 / 60);

                        if (Bhour >= 24) {
                            alert("时间跨度不能超过一天");
                            $("#BtnCommand").trigger('click');
                        }

                        if (nowSdate != "NaN" && nowEdate != "NaN" && nowSdate >= nowEdate) {
                            alert("开始时间不可大于结束时间");
                            $("#BtnCommand").trigger('click');
                        }


                    }, 100)
                }
                else {
                    $.messager.alert("提示", "请选择命令.");
                }
            });
        }
    }

        

    //设置查询定时器
    setInterval(function () {
        var DAUNo = $MCC.GetValue("SearchDAUNo");
        if (DAUNo != null) {
            //刷新数据
            var RTUrl = "../../../Service/Expand/ReverseControl/ControlConsole/GetAllSendCurrent.ashx";
            var RTData = "DAUNo=" + DAUNo;
            $MRT.Ajax(true, RTUrl, RTData, function (xmlData) {
                var Json = $MJO.GetObject(xmlData);
                if (Json != null && Json.Successful != false) {
                    //填充数据
                    var LoadData = $MJO.GetObject('{ "total": ' + Json.Data.length + ', "rows": [] }');
                    LoadData.rows = Json.Data;
                    //载入数据
                    $('#DivStatus').datagrid('loadData', LoadData);
                }
            });
        }
    }, 2000);

    //添加卸载窗口事件
    $MET.UnLoad.Install(function () {
        $MCC.Remove("SearchDAUNo");
        $MCC.Remove("SearchDAUMN");
        $MCC.Remove("SearchPWD");
    });

    setTimeout(' $MEM("BtnSearch").onclick();', 1000);
  
});

//发送反控命令
function SendCtrl(DAUNo, CommandCN,CommandContent) {
    
    var RTUrl = "../../../Service/Expand/ReverseControl/ControlConsole/SendCtrl.ashx"
    var CommandNo = $MEM("SeltCommand").value;
    if (CommandNo != null && CommandNo != -1) {
        $MEM("DivCommand").innerHTML = "载入中...";
        var RT = "../../../Service/Expand/ReverseControl/ControlConsole/GetMarkerCommand.ashx";
        var RTD = "No=" + CommandNo;
        $MRT.Ajax(false, RT, RTD, function (xmlData) {
            var Json1 = $MJO.GetObject(xmlData);
            if (Json1 != null && Json1.Successful != false) {
                CommandCN = Json1.Data.CommandCN;
            }
        });
    }

    var RTData = "DAUNo=" + document.getElementById("DAUNo").value
    RTData += "&CommandCN=" + CommandCN;
    //由于字符串中包含铭感字符 & 加密数据并传输，但在后台需要添加相应的解密
    RTData += "&CommandContent=" + Mtopt.Escape(CommandContent);
    $MRT.Ajax(true, RTUrl, RTData, function (xmlData) {
        var Json = $MJO.GetObject(xmlData);
        if (Json != null && Json.Successful == true) {
            $("#BtnCommand").trigger('click');
        }
        else {
            $.messager.alert("提示", "发送命令失败.");
        }
    });



 
}


